package edu.sctu.jsoup.service;

import edu.sctu.jsoup.entity.AllBbsEntity;
import edu.sctu.jsoup.entity.ClubEntity;
import edu.sctu.jsoup.repo.AllBbsRepo;
import edu.sctu.jsoup.repo.ClubRepo;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class AllBbsService {

    @Autowired
    private ClubRepo clubRepo;

    @Autowired
    private AllBbsRepo allBbsRepo;

    public void run() throws IOException, InterruptedException {

        AllBbsEntity allBbsEntity = new AllBbsEntity();

        /*得到第一张表存入一个集合,下标从0开始*/
        List<ClubEntity> clubEntities = (List<ClubEntity>) clubRepo.findAll();
        //List<AllBbsEntity> allBbsEntities = allBbsRepo.findAll();


        /*
        * 胡干航0~199
        * 何鑫200~399
        * 邱义成400~599
        * 付陈淋600~799
        * 袁超800~999
        * 陈开海1000~1199
        * 易佳1200以后
        *
        * 例如：胡干航修改如下(注意后面的“=”号不要漏掉)
        * for (int j = 0; j <=199; j++)
        *
        * */

        /*获取断点*/
        File filename = new File("D:\\dev\\getTable2.txt");

        InputStreamReader reader = new InputStreamReader(
                new FileInputStream(filename));

        BufferedReader br = new BufferedReader(reader);

        String line = br.readLine();

        int start = Integer.parseInt(line);//将读取到的字符串转化为int类型


        for (int j = start; j <= 1488; j++) {//clubEntities.size()


            /*一秒钟请求一个网页*/
            Thread.currentThread().sleep(1000);


            /*每50次请求后暂停30秒后再继续请求*/
            if (j % 50 == 0) {
                System.out.println("===============休息30秒===============");
                Thread.currentThread().sleep(30000);
            }


            String url = clubEntities.get(j).getUrl();
            Connection connect = Jsoup.connect("http://info.bet007.com/cn/team/Summary.aspx?TeamID=35");
            Map<String, String> header = new HashMap<String, String>();
            header.put("Host", "http://info.bet007.com");
            header.put("User-Agent", "  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0");
            header.put("Accept", "  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            header.put("Accept-Language", "zh-cn,zh;q=0.5");
            header.put("Accept-Charset", "  GB2312,utf-8;q=0.7,*;q=0.7");
            header.put("Connection", "keep-alive");
            Connection data = connect.data(header);
            Document document = Jsoup.connect(url).data(header).get();
        /*
        * 获取论坛帖子总共页数
        * */
            String totalPageText = document.select("span.fr").text();
            String totalPageNum = totalPageText.substring(
                    totalPageText.indexOf("共") + 1, totalPageText.indexOf("页")
            );
            int totalNum = Integer.parseInt(totalPageNum);


        /*
        * 构造每一页的URL,页数随i值变化
        * */

            for (int i = 1; i <= totalNum; i++) {

                AllBbsEntity allBbsEntity2 = new AllBbsEntity();

                    /*
                    * 通过表一中论坛首页URL构造每一页的URL
                    * 存在的问题：
                    * 构造的URL漏掉最后一页URL以及错误构造
                    * */
                url = clubEntities.get(j).getUrl().
                        substring(0, clubEntities.get(j).
                                getUrl().length() - 6) + i + ".html";

                    /*
                    * 不向服务器请求网页只为得到表二
                    * 只需保存页码URL以及Title等信息
                    * */
                // document = Jsoup.connect(url).get();
                allBbsEntity2.setHtml_content("对应第一张表的第" + (j + 1) + "个记录");
                allBbsEntity2.setUrl(url);
                allBbsEntity2.setPage("第" + i + "页/" + "共" + totalNum + "页");
                allBbsEntity2.setTitle(clubEntities.get(j).getTitle());
                allBbsRepo.save(allBbsEntity2);
            }


          /*存储下次请求的开始值或断点*/
            File writename = new File("D:\\dev\\getTable2.txt");

            writename.createNewFile(); // 创建新文件

            BufferedWriter out = new BufferedWriter(new FileWriter(writename));

            out.write(Integer.toString(j + 1));

            // 把缓存区内容压入文件
            out.flush();
            // 最后关闭文件
            out.close();
            System.out.println("下次请求的开始值已记录！");

        }


    }
}
